<?php
/* Ceci est censé élever la limite d'upload des fichiers admise par PHP. En effet, la valeur maxfilesize du formulaire
n'a d'effet que sur le navigateur ! Elle n'a pas d'effet sur PHP qui reste tenu par sa propre limite.
L'instruction ci-dessous est censée élever la limite de PHP, mais elle ne semble pas fonctionner. 
Il semble être nécessaire de modifier la valeur de la propriété < memory_limit > dans le fichier < php.ini >.
*/
$phplimit = config("maxfilesize") / 1048576;
ini_set("memory_limit",$phplimit."M");

?>

<div id="fi_operation_form">
<?php
//###################################################
//###########   NOUVELLE FICHE   ####################
//###################################################
if(!isset($_GET['op']))
{
?>
<form id="form1" name="form1" method="post" action="index.php?a=fi&sa=new&d=fi" enctype="multipart/form-data">
<table width="90%" border="0" align="center" cellspacing="10">
  <tr>
    <td colspan="2" class="newop"><div align="center"><?php say("fi_op_new"); ?></div></td>
    </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_nom");?></div></td>
    <td>
		<div style="float:left;"><input name="nom" type="text" id="nom" size="30" maxlength="255" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'nom');">
			<option>-</option>
			<?php
			// Liste des NOMS des opérations précédentes
			$sql = "SELECT DISTINCT nom FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['nom'])."</option>";
			?>
			</select>
		</div>
	</td>

  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_montant");?></div></td>
    <td><input name="montant" type="text" id="montant" size="15" />
		<label><input type="radio" name="type" value="+" id="type_0" onchange="javascript:ColorMontant(this);" />
          <?php say("fi_op_+"); ?></label>
        <label><input type="radio" name="type" value="-" id="type_1" onchange="javascript:ColorMontant(this);" />
          <?php say("fi_op_-"); ?></label>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_ordre");?></div></td>
    <td>
		<div style="float:left;"><input name="ordre" type="text" id="ordre" size="30" maxlength="255" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'ordre');">
			<option>-</option>
			<?php
			// Liste des ORDRES des opérations précédentes
			$sql = "SELECT DISTINCT ordre FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['ordre'])."</option>";
			?>
			</select>
		</div>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_datef");?></div></td>
    <td><input type="text" class="datechooser dc-dateformat='d/m/Y' dc-iconlink='lib/js/datechooser/datechooser.png' dc-weekstartday='1'  dc-onupdate='datechooser'" id="date_f" name="date_f" value="" size="10" maxlength="10">
	&nbsp;&nbsp;<input type="button" onclick="javascript:document.getElementById('date_f').value='<?php echo date("d/m/Y");?>';"
						style="font-size:0.7em;" value="<?php say("fi_date_today");?>">
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_datee");?></div></td>
    <td><input type="text" class="datechooser dc-dateformat='d/m/Y' dc-iconlink='lib/js/datechooser/datechooser.png' dc-weekstartday='1'  dc-onupdate='datechooser'" id="date_e" name="date_e" value="" size="10" maxlength="10">
		&nbsp;&nbsp;<input type="button" onclick="javascript:document.getElementById('date_e').value='<?php echo date("d/m/Y");?>';"
							style="font-size:0.7em;" value="<?php say("fi_date_today");?>">
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_compte");?></div></td>
    <td>
		<select name="compte" id="compte">
			<?php
			/* Obtenir la liste des comptes */
			$sql = "SELECT id,nom FROM fi_comptes;";
			$connexion = new Connexion();
			$req = $connexion->query($sql);
			while($d = $connexion->fetch_assoc($req))
				echo "<option value=".stripslashes($d['id']).">".$d['nom']."</option>";
			?>
    	</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_moyen");?></div></td>
    <td>
		<select name="moyen" id="moyen">
			<?php
			/* Obtenir la liste des moyens de paiement */
			$sql = "SELECT id,moyen FROM fi_moyens;";
			$connexion = new Connexion();
			$req = $connexion->query($sql);
			while($d = $connexion->fetch_assoc($req))
				echo "<option value=".$d['id'].">".stripslashes($d['moyen'])."</option>";
			?>
    	</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_categorie");?></div></td>
    <td>
			<?php
			/* Obtenir la liste des catégories */
			$sql = "SELECT id,nom FROM fi_categories;";
			$connexion = new Connexion();
			$req = $connexion->query($sql);
			$indice = 1; $display = "";
			while($d = $connexion->fetch_assoc($req))
				{ $display .= "<option value=".$d['id'].">".stripslashes($d['nom'])."</option>"; $indice = $d['id']; }
			$indice++;
			?>
		<div style="float:left;">
		<select name="categories" id="categories" onchange="fi_newcat(<?php echo $indice;?>);">
			<?php echo $display; ?>
			<option value=""></option>
			<option value="<?php echo $indice; ?>">&lt; <?php say("fi_op_cat_new");?> &gt;</option>
    	</select>
		</div>
		<div id="newcat" style="display:none;">
			&nbsp;=>&nbsp;
			<input name="newcategorie" type="text" id="newcategorie" size="20" />
			<input name="isnewcategorie" type="hidden" id="isnewcategorie" value="0" />
		</div>
    </td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_inter");?></div></td>
	<td>
		<div style="float:left;"><input name="intermediaire" type="text" id="intermediaire" size="30" maxlength="255" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'intermediaire');">
			<option>-</option>
			<?php
			// Liste des INTERMÉDIAIRES des opérations précédentes
			$sql = "SELECT DISTINCT intermediaire FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['intermediaire'])."</option>";
			?>
			</select>
		</div>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_label");?></div></td>
    <td>
		<select style="width:40%;" name="labels[]" multiple size="5">
			<?php
			/* Obtenir la liste des labels */
			$sql = "SELECT id,nom FROM labels;";
			$connexion = new Connexion();
			$req = $connexion->query($sql);
			while($d = $connexion->fetch_assoc($req))
				echo "<option value=".$d['id'].">".stripslashes($d['nom'])."</option>";
			?>
		</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_commentaires");?></div></td>
    <td><textarea name="commentaires" style="width:60%;" id="commentaires" rows="5"></textarea></td>
  </tr>
  <tr>
	<td><div align="right"><?php say("fi_op_facture");?></div></td>
	<td style="font-size:0.7em;">
		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo config("maxfilesize"); ?>">
		<input name="facture" type="file" id="facture" style="width:50%;">
		<br />Max : <?php echo config("maxfilesize")/1048576; ?> MB
	</td>
  </tr>
  <tr>
    <td colspan="2">
      <div align="center">
        <input type="submit" name="submit" id="submit" value="<?php say("fi_op_enregistrer");?>" />
        </div></td>
    </tr>
</table>
</form>
<?php
}
//###################################################
//########### AFFICHER UNE FICHE ####################
//###################################################
elseif($_GET['op'] == 'show' and isset($_GET['id']))
{
	$id = $_GET['id'];
	
	$connexion = new Connexion();
	$sql = "SELECT * FROM fi_operations WHERE id='$id';";
	$data = $connexion->fetch_assoc($connexion->query($sql));
	
	/* Facture */
	$FID = $data['factureID'];
	if(is_numeric($FID) and $FID != 0)
	{
		$sql2 = "SELECT nom,taille,type FROM fi_factures WHERE id='$FID'";
		$d = $connexion->fetch_assoc( $connexion->query( $sql2 ) );
		$Fnom 		= stripslashes($d['nom']);
		$Ftaille 	= $d['taille'];
		$Ftype 		= $d['type'];
	}
	
	// Formater les tailles des fichiers attachés
	// Go
	if(isset($Ftaille) and $Ftaille > 1073741824)
	{
		$Ftaille = round($Ftaille / 1073741824 , 2);
		$Ftaille .= " Gb";
	}
	// Mo
	elseif(isset($Ftaille) and $Ftaille > 1048576)
	{
		$Ftaille = round($Ftaille / 1048576 , 2);
		$Ftaille .= " Mb";
	}
	// Ko
	elseif(isset($Ftaille) and $Ftaille > 1024)
	{
		$Ftaille = round($Ftaille / 1024 , 2);
		$Ftaille .= " Kb";
	}
	
	// Formater le montant avec le séparateur des décimales choisi par l'utilisateur
	$montant = str_replace('.',config('sepdec'),$data['montant']);
?>
<!-- Retour à la liste -->
<div class="back">
	<?php if( isset($_GET['x']) and is_numeric($_GET['x']) ) $xback = "&x=".$_GET['x']; else $xback = ""; ?>
	<a href="index.php?d=fi<?php echo $xback; ?>"><?php say('back'); ?></a>
</div>
<!-- Détails de la fiche -->
<form id="form1" name="form1" method="post" action="index.php?a=fi&sa=mod&id=<?php echo $id; ?>&d=fi" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<table width="90%" border="0" align="center" cellspacing="10">
  <tr>
    <td colspan="2"><div align="center"><?php /*say("fi_op_new");*/ ?></div></td>
    </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_nom");?></div></td>
    <td>
		<div style="float:left;"><input name="nom" type="text" id="nom" size="30" value="<?php echo stripslashes($data['nom']); ?>" maxlength="255" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'nom');">
			<option>-</option>
			<?php
			// Liste des NOMS des opérations précédentes
			$sql = "SELECT DISTINCT nom FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['nom'])."</option>";
			?>
			</select>
		</div>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_montant");?></div></td>
    <td><input name="montant" type="text" id="montant" size="20" value="<?php echo $montant; ?>" 
				class="<?php if($montant > 0) echo "colorpos"; else echo "colorneg"; ?>" />
		<label><input type="radio" name="type" value="+" id="type_0" onchange="javascript:ColorMontant(this);" 
				<?php if($montant > 0) echo 'checked'; ?> />
          <?php say("fi_op_+"); ?></label>
        <label><input type="radio" name="type" value="-" id="type_1" onchange="javascript:ColorMontant(this);" 
				<?php if($montant <= 0) echo 'checked'; ?> />
          <?php say("fi_op_-"); ?></label>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_ordre");?></div></td>
    <td>
		<div style="float:left;"><input name="ordre" type="text" id="ordre" size="30" value="<?php echo stripslashes($data['ordre']); ?>" maxlength="255" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'ordre');">
			<option>-</option>
			<?php
			// Liste des ORDRES des opérations précédentes
			$sql = "SELECT DISTINCT ordre FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['ordre'])."</option>";
			?>
			</select>
		</div>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_datef");?></div></td>
    <td><input type="text" class="datechooser dc-dateformat='d/m/Y' dc-iconlink='lib/js/datechooser/datechooser.png' dc-weekstartday='1'  dc-onupdate='datechooser'" id="date_f" name="date_f" value="<?php echo formater_date($data['date_facturation'],false); ?>" size="10" maxlength="10">
	&nbsp;&nbsp;<input type="button" onclick="javascript:document.getElementById('date_f').value='<?php echo date("d/m/Y");?>';"
						style="font-size:0.7em;" value="<?php say("fi_date_today");?>">
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_datee");?></div></td>
    <td><input type="text" class="datechooser dc-dateformat='d/m/Y' dc-iconlink='lib/js/datechooser/datechooser.png' dc-weekstartday='1'  dc-onupdate='datechooser'" id="date_e" name="date_e" value="<?php echo formater_date($data['date_enregistrement'],false); ?>" size="10" maxlength="10">
		&nbsp;&nbsp;<input type="button" onclick="javascript:document.getElementById('date_e').value='<?php echo date("d/m/Y");?>';"
							style="font-size:0.7em;" value="<?php say("fi_date_today");?>">
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_compte");?></div></td>
    <td>
		<select name="compte" id="compte">
		<?php
		/* Obtenir l'ID du compte */
		$op = new Operation();
		$op->load($data['id']);
		$compteID = $op->get('compte');
		/* Obtenir la liste des comptes */
		$sql = "SELECT id,nom FROM fi_comptes;";
		$connexion = new Connexion();
		$req = $connexion->query($sql);
		while($d = $connexion->fetch_assoc($req))
		{ 	if($d['id'] == $compteID)
				echo "<option value=".$d['id']." selected>".stripslashes($d['nom'])."</option>"; 
			else
				echo "<option value=".$d['id'].">".stripslashes($d['nom'])."</option>"; 
		}
		?>
		</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_moyen");?></div></td>
    <td>
		<select name="moyen" id="moyen">
		<?php
		/* Obtenir l'ID du moyen de paiement */
		$op = new Operation();
		$op->load($data['id']);
		$moyenID = $op->get('moyen');
		/* Obtenir la liste des moyens de paiement */
		$sql = "SELECT id,moyen FROM fi_moyens;";
		$connexion = new Connexion();
		$req = $connexion->query($sql);
		while($d = $connexion->fetch_assoc($req))
		{ 	if($d['id'] == $moyenID)
				echo "<option value=".$d['id']." selected>".stripslashes($d['moyen'])."</option>"; 
			else
				echo "<option value=".$d['id'].">".stripslashes($d['moyen'])."</option>"; 
		}
		?>
		</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_categorie");?></div></td>
    <td>
		<?php
		/* Obtenir la liste des catégories */
		$sql = "SELECT id,nom FROM fi_categories;";
		$connexion = new Connexion();
		$req = $connexion->query($sql);
		$indice = 1; $display = "";
		while($d = $connexion->fetch_assoc($req))
			{ 	if($data['categories'] == $d['id'])
					$display .= "<option value=".$d['id']." selected>".stripslashes($d['nom'])."</option>"; 
				else
					$display .= "<option value=".$d['id'].">".stripslashes($d['nom'])."</option>"; 
				$indice = $d['id'];
			}
		$indice++;
		?>
		<div style="float:left;">
		<select name="categories" id="categories" onchange="fi_newcat(<?php echo $indice;?>);">
			<?php echo $display; ?>
			<option value="<?php echo $indice; ?>">&lt; <?php say("fi_op_cat_new");?> &gt;</option>
    	</select>
		</div>
		<div id="newcat" style="display:none;">
			&nbsp;=>&nbsp;
			<input name="newcategorie" type="text" id="newcategorie" size="20" />
			<input name="isnewcategorie" type="hidden" id="isnewcategorie" value="0" />
		</div>
    </td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_inter");?></div></td>
	<td>
		<div style="float:left;"><input name="intermediaire" type="text" id="intermediaire" size="30" maxlength="255" value="<?php echo $data['intermediaire']; ?>" /></div>
		<div>&nbsp;<=&nbsp;
			<select class="verysmall" onchange="selecttoinput(this,'intermediaire');">
			<option>-</option>
			<?php
			// Liste des INTERMÉDIAIRES des opérations précédentes
			$sql = "SELECT DISTINCT intermediaire FROM fi_operations ORDER BY id DESC LIMIT 0,50";
			$connexion = new Connexion();
			$req = $connexion->query( $sql );
			while($d = $connexion->fetch_assoc( $req ) )
				echo "<option>".stripslashes($d['intermediaire'])."</option>";
			?>
			</select>
		</div>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_label");?></div></td>
    <td>
		<select style="width:40%;" name="labels[]" multiple size="5">
			<?php
			/* Obtenir la liste des labels */
			$sql = "SELECT id,nom FROM labels;";
			$connexion = new Connexion();
			$req = $connexion->query($sql);
			while($d = $connexion->fetch_assoc($req))
				//if(substr_count($data['labels'],$d['id'])<1)
				if(!ereg('([[:<:]]'.$d['id'].'[[:>:]])',$data['labels']))
				echo "<option value=".$d['id'].">".stripslashes($d['nom'])."</option>";
				else
				echo "<option value=".$d['id']." selected>".stripslashes($d['nom'])."</option>";
			?>
		</select>
	</td>
  </tr>
  <tr>
    <td><div align="right"><?php say("fi_op_commentaires");?></div></td>
    <td><textarea name="commentaires" style="width:60%;" rows="5" id="commentaires"><?php echo stripslashes($data['commentaires']); ?></textarea></td>
  </tr>
	<tr>
	<td align="right">
		<?php say("fi_op_facture"); ?>
	</td>
	<td style="font-size:0.9em;">
		<table>
			<tr>
			<td>
				<?php 
				if(is_numeric($FID) and $FID != 0) 
				{
					echo "<span class=\"fichier\" onclick=\"window.location='extdata/facture.php?id=$FID'\""
						.">&nbsp;$Fnom&nbsp;</span><br /> ($Ftype | $Ftaille)";
					echo "<input type=\"hidden\" name=\"factureID\" value=\"$FID\" />";
					echo '<br />';
					?>
						</td>
						<td style="font-size:0.9em;">
					<?php
					say("fi_op_facture_new");
				}
				else 
				{
					?>
						<input type="hidden" name="factureID" value="" />
					</td>
					<td style="font-size:0.9em;">	
					<?php
					
				}
				?> 
				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo config("maxfilesize"); ?>">
				<input name="facture" type="file" id="facture" size=50>
				<br />Max : <?php echo config("maxfilesize")/1048576; ?> MB
			</td>
			</tr>
		</table>
  <tr>
    <td colspan="2">
      <div align="center">
        <input type="submit" name="submit" id="submit" value="<?php say("fi_op_enregistrer");?>" />
        </div></td>
    </tr>
</table>
</form>
<?php
}
elseif($_GET['op'] == 'mod' and isset($_GET['id']))
{
	
}
?>
</div>
